import StringUtils from "./string";
// 与unity通信使用
let UnityIm = {
  /**
   * 接收unity向Web发送打开哪个面板的消息
   * @param handler 接收到消息后的处理方法
   */
  receiveUnityOpenMessage: (handler = null) => {
    window.vuplex.addEventListener("message", event => {
      let json_str = event.data;
      let json_obj = StringUtils.toJson(json_str);
      let params = StringUtils.toJson(json_obj.parameterData || "");
      if (json_obj.managerType == "open") {
        if (handler && typeof handler == "function") {
          handler(json_obj.methodName, params);
        }
      }
    });
  },
  /**
   * 接收unity向Web发送关闭哪个面板的消息
   * @param handler 接收到消息后的处理方法
   */
  receiveUnityCloseMessage: (handler = null) => {
    window.vuplex.addEventListener("message", event => {
      let json_str = event.data;
      let json_obj = StringUtils.toJson(json_str);
      let params = StringUtils.toJson(json_obj.parameterData || "");
      if (json_obj.managerType == "close") {
        if (handler && typeof handler == "function") {
          handler(json_obj.methodName, params);
        }
      }
    });
  },
  /**
   * 接收unity向Web发送取消选择的消息
   * @param handler 接收到消息后的处理方法
   */
  receiveUnityCancelSelectedMessage: (handler = null) => {
    window.vuplex.addEventListener("message", event => {
      let json_str = event.data;
      let json_obj = StringUtils.toJson(json_str);
      let params = StringUtils.toJson(json_obj.parameterData || "");
      if (json_obj.managerType == "cancelSelected") {
        if (handler && typeof handler == "function") {
          handler(json_obj.methodName, params);
        }
      }
    });
  },
  /**
   * 接收unity传送给web处理选择/取消选择资源检索的大小工程消息
   * @param handler 接收到消息后的处理
   */
  receiveUnityHandSelectMessage: (handler = null) => {
    window.vuplex.addEventListener("message", event => {
      let json_str = event.data;
      let json_obj = StringUtils.toJson(json_str);
      let params = StringUtils.toJson(json_obj.parameterData || "");
      if (json_obj.managerType == "HandSelect") {
        if (handler && typeof handler == "function") {
          handler(json_obj.methodName, params);
        }
      }
    });
  },
  /**
   * 接收unity传送给web处加载完成消息
   * @param handler 接收到消息后的处理
   */
  loadingCompletedfun: (handler = null) => {
    window.vuplex.addEventListener("message", event => {
      let json_str = event.data;
      let json_obj = StringUtils.toJson(json_str);
      let params = StringUtils.toJson(json_obj.parameterData || "");
      if (json_obj.managerType == "simulation") {
        if (handler && typeof handler == "function") {
          handler(json_obj.methodName, params);
        }
      }
    });
  },
	
	


  /**
   * 关闭哪个面板
   * @param name
   */
  closePanelManager: name => {
    let _data = {
      PanelName: name
    };
    let data_str = StringUtils.toStr(_data);
    let p = {
      managerType: "PanelManager",
      methodName: "ClosePanel",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 更新 项目工程/智慧河湖-信息标注 的选择
   * @param from
   * @param name
   * @param arr
   */
  refreshInfoMark: (from, name, arr = []) => {
    let initDataArr = [];
    for (let i = 0; i < arr.length; i++) {
      initDataArr.push({
        id: arr[i].name,
        state: arr[i].name == name
      });
    }
    let _data = {
      StateData: initDataArr
    };
    let data_str = StringUtils.toStr(_data);

    let p = {
      managerType: from,
      methodName: "RefeshInfoMark",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 更新 项目工程/智慧河湖-行政区划 的选择
   * @param from
   * @param id
   * @param arr
   */
  refreshEngineerArea: (from, id, arr = []) => {
    let initDataArr = [];
    for (let i = 0; i < arr.length; i++) {
      initDataArr.push({
        id: arr[i].id + "",
        state: typeof id == "number" && arr[i].id != -1 && arr[i].id == id
      });
    }
    let _data = {
      StateData: initDataArr
    };
    let data_str = StringUtils.toStr(_data);
    let p = {
      managerType: from,
      methodName: "RefeshEngineerArea",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 更新 项目工程/智慧河湖-资源检索 的选择
   * @param from
   * @param item
   * @param parent
   */
  refreshHandSelect: (from, item, parent) => {
    let _data = item || {};
    let tF_type = typeof from;
    let fromType = tF_type == "string" ? from : '';
    if (tF_type == "object") {
      from = from.hasOwnProperty('page') ? from.page : '';
      fromType = from.hasOwnProperty('type') ? from.type : '';
    }
    Object.assign(_data, {
      parent_id:
        (fromType == "engineerControlSearch" || fromType == "engineer")
          ? parent.project_type : parent.id
    });
    let data_str = StringUtils.toStr(_data);
    let p = {
      managerType: from,
      methodName: "HandSelect",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 取消 项目工程/智慧河湖-资源检索 的选择
   * @param from
   * @param item
   * @param parent
   */
  refreshHandCancel: (from, item, parent) => {
    let _data = item || {};
    let data_str = StringUtils.toStr(_data);
    let p = {
      managerType: from,
      methodName: "HandCancel",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 项目工程/智慧河湖/智慧防汛-资源检索 父级的选择与取消
   * @param from 项目工程engineerControlSearch/智慧河湖riverControlSearch/智慧防汛floodResourceRetrieval
   * @param parentIds 已选择的父级的id数组
   */
  refreshParentHandSelect: (from, parentIds) => {
    let data_str = StringUtils.toStr(parentIds, '[]');
    let p = {
      managerType: from,
      methodName: "HandSelectParent",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 更新 智慧河湖-监测分析 的选择
   * @param from
   * @param name
   * @param arr
   */
  refreshMonitorAnalysis: (from, name, arr = []) => {
    let initDataArr = [];
    for (let i = 0; i < arr.length; i++) {
      initDataArr.push({
        id: arr[i].name,
        state: arr[i].name == name
      });
    }
    let _data = {
      StateData: initDataArr
    };
    let data_str = StringUtils.toStr(_data);
    let p = {
      managerType: from,
      methodName: "RefeshMonitorAnalysis",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 更新 智慧河湖-工程运行监测-站点筛选 的选择
   * @param ids 已选择的 id 列表
   * @param arr 所有站点筛选的列表
   */
  refreshRiverEngineerFilter: (ids, arr = []) => {
    // let initDataArr = [];
    // for (let i = 0; i < arr.length; i++) {
    //   initDataArr.push({
    //     id: arr[i].name,
    //     state: names.indexOf(arr[i].name) > -1
    //   });
    // }
    // let _data = {
    //   StateData: initDataArr
    // };
    // let data_str = StringUtils.toStr(_data);
    let data_str = StringUtils.toStr(ids);
    let p = {
      managerType: "riverEngineerRun",
      methodName: "RefeshRiverEngineerFilter",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 更新智慧河湖-工程运行监测-全局状态总览的选择
   * @param from
   * @param item
   */
  updateSelectRiverEngineerStatus: (from, item) => {
    let data_str = StringUtils.toStr(item);
    let p = {
      managerType: "riverEngineerRun",
      methodName: "SelectRiverEngineerStatus",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 更新智慧河湖-工程运行监测-全局状态总览的取消选择
   * @param from
   * @param item
   */
  unSelectRiverEngineerStatus: (from, item) => {
    let data_str = StringUtils.toStr(item);
    let p = {
      managerType: "riverEngineerRun",
      methodName: "UnSelectRiverEngineerStatus",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 更新智慧河湖-工程运行监测-预警记录的选择
   * @param from
   * @param item
   */
  updateSelectRiverEngineerEarlyWarn: (from, item) => {
    let data_str = StringUtils.toStr(item);
    let p = {
      managerType: "riverEngineerRun",
      methodName: "SelectRiverEngineerEarlyWarn",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 更新智慧河湖-看板-预警记录的选择
   * @param from
   * @param item
   */
  updateSelectKanBanEarlyWarn: (from, item) => {
    let data_str = StringUtils.toStr(item);
    let p = {
      managerType: "riverKanBanWarn",
      methodName: "SelectRiverBubbleWarn",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 监控页面设置全屏
   * @param from
   * @param item
   */
  videoSetFullWindow: (from, item) => {
    let data_str = StringUtils.toStr(item);
    let p = {
      managerType: "VideoSurveillancePanel",
      methodName: "SetFullWindow",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
  /**
   * 监控页面取消全屏
   * @param from
   * @param item
   */
  videoSetWindow: (from, item) => {
    let data_str = StringUtils.toStr(item);
    let p = {
      managerType: "VideoSurveillancePanel",
      methodName: "SetWindow",
      parameterData: data_str // 字符串类型
    };
    try {
      window.vuplex.postMessage(p);
    } catch (e) {
      window.parent.postMessage(p, '/');
    }
  },
	/**
	 * 监控页面取消全屏
	 * @param from
	 * @param item
	 */
	loadChart: (from, item) => {
	  let data_str = StringUtils.toStr(item);
	  let p = {
	    managerType: "floodChart",
	    methodName: "rainsstorm",
	    parameterData: data_str // 字符串类型
	  };
	  try {
	    window.vuplex.postMessage(p);
	  } catch (e) {
	    window.parent.postMessage(p, '/');
	  }
	}
};

export default UnityIm;
